<?php

namespace Common\Service;


use Think\Log;

/**
 * 发送变量短信
 */
class ChuanglanSmsTpl
{

    /**
     * 创建订单成功给店长发送短信
     * $params = '18571640043,aaa,bbb,ccc,ddd,eee;18571640043,aaa,bbb,ccc,ddd,rrr';
     * @param $order
     * @return mixed|string
     */
    public static function sendActivitySmsToStore($activity_id)
    {
        $activity = M('activity')->where(['id' => $activity_id])->find();
        $audit = M('audit')->where(['id' => $activity['audit_user_id']])->find();
        $stores = M('store')->where(['id' => ['in', $activity['store_ids']]])->select();

        $str = '';
        foreach ($stores as $k => $v) {
            $start_time = date('Y-m-d', $activity['activity_start_time']);
            $end_time = date('Y-m-d', $activity['order_end_time']);
            $order_end_time = date('Y-m-d', $activity['order_end_time']);
            $str .= "{$v['mobile']},{$v['manager_name']},{$audit['name']},{$activity['name']},{$start_time}-{$end_time},{$order_end_time};";
        }

        $params = substr($str, 0, -1);
        $msg = '【盘古之手】尊敬的{$var}店长，审核员{$var}建立了{$var}活动物料下单计划，活动时间：{$var}，物料下单截止时间{$var}，请您及时登录小程序完成物料下单。 ';
        $result = ChuanglanSms::sendVariableSMS($msg, $params);

        if (!is_null(json_decode($result))) {
            $output = json_decode($result, true);
            if (isset($output['code']) && $output['code'] == '0') {
                $ret = '发送成功';
            } else {
                $ret = $output['errorMsg'];
            }
        } else {
            $ret = $result;
        }

        Log::write('创建订单成功给店长发送短信，数据---结果：' . $params . '---' . $result);
        foreach ($stores as $k => $v) {
            smsLog('创建活动', $audit['name'], $v['mobile'], $ret, 1, '', '下单成功推送短信给审核员');
        }
        return $ret;
    }


    /**
     * 下单成功给审核员发送短信
     * $params = '18571640043,aaa,bbb,123456';
     * @param $order
     * @return mixed|string
     */
    public static function sendOrderSmsToAudit($order)
    {
        $store = M('store')->where(['store_id' => $order['store_id']])->find();
        $audit = M('audit')->where(['id' => $store['audit_user_id']])->find();
        $activity = M('activity')->where(['id' => $order['activity_id']])->find();
        $params = $audit['mobile'] . ',' . $store['store_name'] . ',' . $activity['name'] . ',' . $order['order_no'];

        $msg = '【盘古之手】订单审核通知，{$var}店铺的{$var}活动的物料，订单号：{$var}，店长已完成物料下单，请登陆小程序完成审核。';
        $result = ChuanglanSms::sendVariableSMS($msg, $params);
        if (!is_null(json_decode($result))) {
            $output = json_decode($result, true);
            if (isset($output['code']) && $output['code'] == '0') {
                $ret = '发送成功';
            } else {
                $ret = $output['errorMsg'];
            }
        } else {
            $ret = $result;
        }

        Log::write('下单成功给审核员发送短信，数据---结果：' . $params . '---' . $ret);
        smsLog($order['order_no'], $audit['name'], $audit['mobile'], $ret, 1, '', '下单成功推送短信给审核员');
        return $ret;
    }

    /**
     * 发送验证码短信
     * @param $mobile
     * @return bool
     */
    public static function sendCode($mobile)
    {
        $code = SELF::getCode(6);
        $result = ChuanglanSms::sendSMS($mobile, $code);

        if (!is_null(json_decode($result))) {
            $output = json_decode($result, true);
            if (isset($output['code']) && $output['code'] == '0') {
                S($mobile, $code, 600);
                $tmp = '发送成功';
                $ret = true;
                smsLog('', '', $mobile, $tmp, 2, $code, '验证码发送成功');
            } else {
                $tmp = $output['errorMsg'];
                $ret = false;
                smsLog('', '', $mobile, $tmp, 2, $code, '验证码发送失败');
            }
        } else {
            $tmp = $result;
            $ret = false;
            smsLog('', '', $mobile, $tmp, 2, $code, '验证码发送失败');
        }

        return $ret;
    }

    /**
     * 指定长度的数字字符串
     * @param $len
     * @return string
     */
    private static function getCode($len)
    {
        $code = '';
        for ($i = 0; $i < $len; $i++) {
            $randcode = mt_rand(0, 9);
            $code .= $randcode;
        }
        return $code;
    }
}